package com.hk.jmm.utils;

import android.app.Activity;

import com.github.gzuliyujiang.wheelpicker.DatePicker;
import com.github.gzuliyujiang.wheelpicker.DatimePicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;

public class DateDialogUtil {
    //仅日期选择
    public static final int DATE_ONLY = 1;
    //仅日期时间选择 24H
    public static final int DATE_TEIM_24= 2;
    //仅日期时间选择 12H
    public static final int DATE_TEIM_12 = 3;

    private static final DateEntity startDate = DateEntity.target(2000, 1, 1);
    private static final DateEntity endDate = DateEntity.target(2100, 1, 1);


    public static void showDateTime(Activity activity,int type,DatimePicked datimePicked){
        if (type==1){
            showDate(activity,datimePicked);
        }else {
            showDateAndTime(activity,type,datimePicked);
        }
    }

    public static void showDate(Activity activity,DatimePicked datimePicked){
        DatePicker picker = new DatePicker(activity);
        //picker.setBodyWidth(240);
        DateWheelLayout wheelLayout = picker.getWheelLayout();
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
        wheelLayout.setDateLabel("年", "月", "日");
        wheelLayout.setRange(startDate, endDate,DateEntity.today());
//        wheelLayout.setCurtainEnabled(true);
//        wheelLayout.setCurtainColor(0xFFCC0000);
//        wheelLayout.setIndicatorEnabled(true);
//        wheelLayout.setIndicatorColor(0xFFFF0000);
//        wheelLayout.setIndicatorSize(activity.getResources().getDisplayMetrics().density * 2);
//        wheelLayout.setTextColor(0xCCCC0000);
//        wheelLayout.setTextSize(14 * activity.getResources().getDisplayMetrics().scaledDensity);
        //注：建议通过`setStyle`定制样式设置文字加大，若通过`setSelectedTextSize`设置，该解决方案会导致选择器展示时跳动一下
        //wheelLayout.setSelectedTextSize(16 * getResources().getDisplayMetrics().scaledDensity);
//        wheelLayout.setSelectedTextColor(0xFFFF0000);
        //wheelLayout.getYearLabelView().setTextColor(0xFF999999);
        //wheelLayout.getMonthLabelView().setTextColor(0xFF999999);
        picker.setOnDatePickedListener(new OnDatePickedListener() {
            @Override
            public void onDatePicked(int year, int month, int day) {
                String text = year + "-" + (month<10?"0"+month:month) + "-" +  (day<10?"0"+day:day);
                datimePicked.onDatimePicked(text,year,month,day,0,0,0,"");
            }
        });
        picker.getWheelLayout().setResetWhenLinkage(false);
        picker.show();
    }

    private static void showDateAndTime(Activity activity,int type,DatimePicked datimePicked){
        DatimePicker picker = new DatimePicker(activity);
        final DatimeWheelLayout wheelLayout = picker.getWheelLayout();
        picker.setOnDatimePickedListener(new OnDatimePickedListener() {
            @Override
            public void onDatimePicked(int year, int month, int day, int hour, int minute, int second) {
                String text = year + "-" + (month<10?"0"+month:month) + "-" +  (day<10?"0"+day:day) + " " +  (hour<10?"0"+hour:hour) + ":" +  (second<10?"0"+second:second) ;
                String apm = wheelLayout.getTimeWheelLayout().isAnteMeridiem() ? "AM" : "PM";
                datimePicked.onDatimePicked(text,year,month,day,hour,minute,second,apm);
            }
        });
        int timeMode =type==2?TimeMode.HOUR_24_HAS_SECOND:TimeMode.HOUR_12_HAS_SECOND;

        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
        wheelLayout.setTimeMode(timeMode);
        DatimeEntity start = DatimeEntity.now();
        start.setDate(startDate);
        DatimeEntity end = DatimeEntity.now();
        end.setDate(endDate);
        wheelLayout.setRange(start,end,DatimeEntity.now());
//        wheelLayout.setRange(start, DatimeEntity.yearOnFuture(10));
        wheelLayout.setDateLabel("年", "月", "日");
        wheelLayout.setTimeLabel("时", "分", "秒");
        picker.show();
    }

    public interface  DatimePicked{
        public void onDatimePicked(String time,int year, int month, int day, int hour, int minute, int second,String apm);
    }
}
